package com.zju.DoctorGo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.zju.DoctorGo.dto.BillDTO;
import com.zju.DoctorGo.entity.Bill;
import com.zju.DoctorGo.entity.Result;
import com.zju.DoctorGo.service.PaymentService;
import lombok.extern.slf4j.Slf4j;

@CrossOrigin
@RestController
@Slf4j
public class BillController {
    @Autowired
    private PaymentService paymentService;


    @PostMapping("/api/bill")
    public Result createBill(@RequestBody BillDTO billDTO) {
        log.info("Creating bill: {}", billDTO);
        try {
            paymentService.createBill(billDTO);
            return Result.success();
        } catch (DuplicateKeyException e) {
            log.error("Failed to create bill due to duplicate key: {}", e.getMessage());
            // Return a 409 Conflict status, indicating a duplicate key error
            return Result.error("A bill with the same ID already exists. Please use a unique ID.");
        } catch (Exception e) {
            log.error("Failed to create bill: {}", e.getMessage());
            // Return a 500 Internal Server Error status
            return Result.error("An unexpected error occurred while creating the bill.");
        }
    }

    @GetMapping("/api/bill/{billId}")
    public Result getBill(@PathVariable String billId) {
        try {
            // Assuming you have a service called billService with a deleteBill method
            Bill bill = paymentService.getBillById(billId);
            if (bill != null) {
                return Result.success(bill);
            } else {
                return Result.error("fail to get, bill doesn't exist");
            }
        } catch (Exception e) {
            // Log the exception details
            return Result.error("An error occurred while get the bill");
        }
    }

    @GetMapping("/api/bill")
    public Result queryBill(@RequestParam("userId") int userId) {
        log.info("查询用户账单，用户ID: {}", userId);
        try {
            List<Bill> bills = paymentService.selectBillsByPaymentPartyId(userId);
            System.out.println("bill"+bills);
            return Result.success(bills);
        } catch (Exception e) {
            log.error("查询账单失败: {}", e.getMessage());
            return Result.error(e.toString());
        }
    }

    @GetMapping("/api/bill/querybyDoctor")
    public Result queryBill2(@RequestParam("doctorId") int doctorId) {
        log.info("查询用户账单，用户ID: {}", doctorId);
        try {
            List<Bill> bills = paymentService.selectBillsByInitiatorId(doctorId);
            System.out.println("bill"+bills);
            return Result.success(bills);
        } catch (Exception e) {
            log.error("查询账单失败: {}", e.getMessage());
            return Result.error(e.toString());
        }
    }

    @DeleteMapping("/api/bill/{billId}")
    public Result deleteBill(@PathVariable String billId) {
        try {
            // Assuming you have a service called billService with a deleteBill method
            int isDeleted = paymentService.deleteBillById(billId);
            if (isDeleted != 0) {
                return Result.success();
            } else {
                return Result.error("fail to delete, bill doesn't exist");
            }
        } catch (Exception e) {
            // Log the exception details
            return Result.error("An error occurred while deleting the bill");
        }
    }

    @PutMapping("/api/bill")
    public Result updateBill(@RequestBody BillDTO billDTO) {
        log.info("更新账单: {}", billDTO);
        try {
            paymentService.updateBill(billDTO);
            return Result.success();
        } catch (Exception e) {
            log.error("Failed to update bill: {}", e.getMessage());
            return Result.error("An unexpected error occurred while updating the bill.");
        }
    }

}
